package com.xiaomili.halfsugar.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.activity.SearchActivity;
import com.xiaomili.halfsugar.adapter.ViewPagerAdapter;
import com.xiaomili.halfsugar.bean.HomeBean;
import com.xiaomili.halfsugar.constant.UrlString;
import com.xiaomili.halfsugar.utils.OkHttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class HomeFragment extends Fragment {
    private List<String> mCategoryList;
    private static final String TAG = "HomeFragment";
    private ViewPager mVp;
    private ViewPagerAdapter mVpAdapter;
    private TabLayout mTabLayout;
    //    private FlashView mFlashView;
    private Toolbar mToolbar;
    private PtrClassicFrameLayout mPtrFl;
    private AppBarLayout mAppBarLayout;
    private TextView mTextView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    //初始化控件
    private void initView(View view) {
        mVp = (ViewPager) view.findViewById(R.id.vp);
        mAppBarLayout = (AppBarLayout) view.findViewById(R.id.appBarLayout);
        mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        mTextView = (TextView) view.findViewById(R.id.tv);
//        mFlashView = (FlashView) view.findViewById(R.id.flashView);
        mToolbar = (Toolbar) view.findViewById(R.id.top_toolBar);
        mPtrFl = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_fl);
        mPtrFl.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                initData();
                mPtrFl.refreshComplete();
            }
        });
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), SearchActivity.class));
            }
        });
        if (mAppBarLayout != null)
            mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    if (mPtrFl == null) return;
                    mPtrFl.setEnabled(verticalOffset >= 0 ? true : false);
                }
            });
    }

    //初始化数据
    private void initData() {
        mCategoryList = new ArrayList<>();
        OkHttpUtil.newInstance(getActivity()).getAsyncData(UrlString.INDEX_URL[0], new OkHttpUtil.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                Gson gson = new Gson();
                HomeBean homeBean = gson.fromJson(result, HomeBean.class);
                int mCategoryListSize = homeBean.data.category_element.size();
                for (int i = 0; i < mCategoryListSize; i++) {
                    mCategoryList.add(homeBean.data.category_element.get(i).title);
                }
                mVpAdapter = new ViewPagerAdapter(getFragmentManager(), mCategoryList);
                mVp.setAdapter(mVpAdapter);
                mTabLayout.setupWithViewPager(mVp);
//                mVp.setOffscreenPageLimit(9);
                mVpAdapter.notifyDataSetChanged();

                List<String> imageUrls = new ArrayList<>();
                Log.i(TAG, "onResponse: " + homeBean.data.banner.size());
//                for (int i = 0; i < homeBean.data.banner.size(); i++) {
//                    imageUrls.add(homeBean.data.banner.get(i).photo);
//                }
//                mFlashView.setImageUris(imageUrls);
            }
        });
    }
}
